package com.tarena.lbs.business.servcie.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.dao.mapper.BusinessMapper;
import com.tarena.lbs.business.dao.mapper.StoreMapper;
import com.tarena.lbs.business.pojo.param.StoreParam;
import com.tarena.lbs.business.pojo.po.StorePO;
import com.tarena.lbs.business.pojo.query.AreaStoreQuery;
import com.tarena.lbs.business.pojo.query.StoreQuery;
import com.tarena.lbs.business.pojo.vo.StoreVO;
import com.tarena.lbs.business.servcie.StoreService;
import com.tarena.lbs.common.enums.PassportResultEnum;
import com.tarena.lbs.common.enums.Roles;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.common.utils.AuthenticationContextUtils;
import com.tarena.lbs.user.api.UserApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class StoreServiceImpl implements StoreService {
	
	@Autowired
	private StoreMapper storeMapper;
	@Autowired
	private BusinessMapper businessMapper;
	@DubboReference
	private UserApi userApi;
	
	// 新增店铺
	@Override
	public void save(StoreParam param) throws BusinessException {
		log.debug(">>>新增店铺-业务-入参：{}", param);
		
		// 获取当前登录用户
		UserPrinciple userPrinciple = AuthenticationContextUtils.get();
		// 检查登录状态
		Asserts.isTrue(userPrinciple == null, new BusinessException(PassportResultEnum.NOT_LOGIN));
		// 检查用户角色
		Asserts.isTrue(userPrinciple.getRole() != Roles.SHOP, new BusinessException(PassportResultEnum.NOT_PERMISSION));
		// 检查商家是否存在
		Asserts.isTrue(businessMapper.getDetailById(param.getBusinessId()) == null, new BusinessException(PassportResultEnum.BUSINESS_NOT_EXIST));
		// TODO 检查店铺名是否存在
		// 补充保存参数
		StorePO po = new StorePO();
		BeanUtils.copyProperties(param, po);
		po.setCreateTime(new Date());
		// TODO 需添加所属省ID
		// 保存数据，并返回新增的店铺ID
		storeMapper.save(po);
		// TODO 绑定图片
		// 获取新增店铺的ID
		Integer id = po.getId();
	}
	
	
	// 分页查询店铺列表
	@Override
	public PageResult<StoreVO> getPageList(StoreQuery query) throws BusinessException {
		log.debug(">>>查询店铺列表-业务-入参：{}", query);
		
		// 获取当前登录用户
		UserPrinciple userPrinciple = AuthenticationContextUtils.get();
		// 检查登录状态
		Asserts.isTrue(userPrinciple == null, new BusinessException(PassportResultEnum.NOT_LOGIN));
		// 获取当前登录用户信息
		Roles role = userPrinciple.getRole();
		Integer businessId = userPrinciple.getId();
		// 判断用户角色，平台管理员可查询所有店铺，商家管理员可查询自己店铺
		if (role == Roles.SHOP) {
			// 检查商家是否存在
			Asserts.isTrue(businessMapper.getDetailById(businessId) == null, new BusinessException(PassportResultEnum.BUSINESS_NOT_EXIST));
			// 将商家ID设置为查询条件
			query.setBusinessId(businessId);
		}
		
		// 分页查询
		PageResult<StoreVO> pageResult = new PageResult<>(query);
		PageHelper.startPage(query.getPageNo(), query.getPageSize());
		List<StorePO> poList = storeMapper.getPageList(query);
		// 封装分页信息
		PageInfo<StorePO> pageInfo = new PageInfo<>(poList);
		// 封装分页结果
		pageResult.setPageNo(query.getPageNo());
		pageResult.setPageSize(query.getPageSize());
		pageResult.setTotal(pageInfo.getTotal());
		List<StoreVO> vos = null;
		if (CollectionUtils.isNotEmpty(poList)) {
			vos = poList.stream().map(po -> {
				StoreVO vo = new StoreVO();
				BeanUtils.copyProperties(po, vo);
				return vo;
			}).collect(Collectors.toList());
		}
		pageResult.setObjects(vos);
		// 返回分页结果
		return pageResult;
	}
	
	// 分页查询区域下店铺列表
	@Override
	public PageResult<StoreVO> getStoreByCity(AreaStoreQuery query) throws BusinessException {
		log.debug(">>>查询区域下的店铺列表-业务-入参：{}", query);
		// 获取当前登录用户
		UserPrinciple userPrinciple = AuthenticationContextUtils.get();
		// 检查登录状态
		Asserts.isTrue(userPrinciple == null, new BusinessException(PassportResultEnum.NOT_LOGIN));
		// 获取登录用户ID
		Integer id = userPrinciple.getId();
		
		// 分页查询
		PageResult pageResult = new PageResult(query);
		PageHelper.startPage(query.getPageNo(), query.getPageSize());
		List<StorePO> poList = storeMapper.selectListByCityAndBusiness(query, id);
		PageInfo<StorePO> pageInfo = new PageInfo<>(poList);
		pageResult.setPageNo(query.getPageNo());
		pageResult.setPageSize(query.getPageSize());
		pageResult.setTotal(pageInfo.getTotal());
		List<StoreVO> vos = null;
		if (CollectionUtils.isNotEmpty(poList)) {
			vos = poList.stream().map(po -> {
				StoreVO vo = new StoreVO();
				BeanUtils.copyProperties(po, vo);
				return vo;
			}).collect(Collectors.toList());
		}
		pageResult.setObjects(vos);
		
		return pageResult;
	}
	
}
